﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MegavaniaX.World
{
    [Serializable]
    public struct Transistion
    {
        /// <summary>
        /// these are "screenwidth" instances!  so (1,0) and horiz=true means blocks 16-31 with y = 0
        /// </summary>
        public int FromX, FromY;

        public int ToX, ToY;

        public string NewScreenID;

        /// <summary>
        /// 'T' 'R' 'B' 'L'
        /// </summary>
        public char Side;

        public Transistion(int fromX, int fromY, int toX, int toY, string newScreenID, char side)
        {
            if (side != 'T' && side != 'B' && side != 'L' && side != 'R')
                throw new MegavaniaException(string.Format("Unknown transistion side {0}", side));
            FromX = fromX;
            FromY = fromY;
            ToX = toX;
            ToY = toY;
            NewScreenID = newScreenID;
            Side = side;
        }

        public IList<int> GetXRange()
        {

            var intList = new List<int>();
            if (Side == 'R' || Side == 'L') // only 1 column of tiles we care about, so 1 X value
            {
                int theValue = 0;
                if (Side == 'L')
                {
                    theValue = FromX * Screen.ScreenWidth;
                }
                else // 'R'
                {
                    theValue = ((FromX + 1) * Screen.ScreenWidth) - 1;
                }
                intList.Add(theValue);
                return intList;
            }
            else // T or B, whole row of X values
            {
                int start = Screen.ScreenWidth * FromX;
                int end = Screen.ScreenWidth * (FromX + 1) - 1;
                for (int i = start; i < end; i++)
                {
                    intList.Add(i);
                }
            }
            return intList;
        }

        public IList<int> GetYRange()
        {
            var intList = new List<int>();
            if (Side == 'T' || Side == 'B') // only 1 row of tiles we care about, so 1 Y value
            {
                int theValue = 0;
                if (Side == 'T')
                {
                    theValue = FromY * Screen.ScreenHeight;
                }
                else // 'B'
                {
                    theValue = ((FromY + 1) * Screen.ScreenHeight) - 1;
                }
                intList.Add(theValue);
                return intList;
            }
            else // R or L, whole row of Y values
            {
                int start = FromY * Screen.ScreenHeight;
                int end = ((FromY + 1) * Screen.ScreenHeight) - 1;
                for (int i = start; i < end; i++)
                {
                    intList.Add(i);
                }
            }
            return intList;
        }

        public override bool Equals(object obj)
        {
            if (!(obj is Transistion))
                return false;
            Transistion o = (Transistion)obj;
            return (FromX == o.FromX && o.FromY == FromY
                    && ToX == o.ToX && ToY == o.ToY &&
                    NewScreenID == o.NewScreenID && Side == o.Side);
        }

        public override int GetHashCode()
        {
            return FromX.GetHashCode() * 3 + FromY.GetHashCode() * 7 + ToX.GetHashCode() * 23 + ToY.GetHashCode() * 13 + Side.GetHashCode() * 5 + NewScreenID.GetHashCode() * 11;
        }

    }
}
